//
//  MQVistorView.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/13.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

protocol MQVistorViewDelegate:NSObjectProtocol{
    func loginButtonDidClick()
    func registerButtonDidClick()
}

class MQVistorView: UIView {
    
    //代理
    weak var delegate:MQVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    // MARK: - 外部接口
    func setupInfo(imageName:String? = nil, title:String? = nil){
        if imageName != nil {
            circleIcon.hidden = true
            houseImage.image = UIImage(named: imageName!)
            messageLabel.text = title
        }else{
            startAnimation()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
// MARK: - 懒加载控件
    //house image
    private lazy var houseImage:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    
    //旋转image
    private lazy var circleIcon:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮挡image
    private lazy var maskImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //消息label
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        label.textAlignment = .Center
        return label
    }()
    
    //登录btn
    private lazy var loginBtn:UIButton = {
        let btn = UIButton(textColor: UIColor.darkTextColor(), fontSize: 14)
        btn.addTarget(self, action: "loginBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()
    
    //注册btn
    private lazy var registerBtn:UIButton = {
        let btn = UIButton(textColor: UIColor.darkTextColor(), fontSize: 14)
        btn.addTarget(self, action: "registerBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()
    
    //-------------------------------------------------------------------------
    private lazy var message:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_image_message"))
    
    private lazy var profile:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_image_profile"))

}

// MARK: - 设置UI界面
extension MQVistorView{
    //SnapKit设置约束
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleIcon)
        addSubview(maskImage)
        
        addSubview(houseImage)
        addSubview(messageLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        houseImage.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
            make.center.equalTo(snp_center)
        }
        circleIcon.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
            make.center.equalTo(snp_center)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self.snp_centerX)
//            make.centerY.equalTo(self.snp_centerY)
            make.center.equalTo(snp_center)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginBtn.snp_top)
            make.width.equalTo(loginBtn)
        }
        
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 20
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        //切换界面时不会停止动画
        anim.removedOnCompletion = false
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
}

// MARK: - btn点击事件
extension MQVistorView{
    @objc private func registerBtnDidClick(btn:UIButton){
        delegate?.registerButtonDidClick()
    }
    
    @objc private func loginBtnDidClick(btn:UIButton){
        delegate?.loginButtonDidClick()
    }
}

// MARK: - 原生方法设置约束
extension MQVistorView{
    private func setupUIDemo(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleIcon)
        addSubview(maskImage)
        
        addSubview(houseImage)
        addSubview(messageLabel)
        
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // > 1. 大图标
        houseImage.translatesAutoresizingMaskIntoConstraints = false
        
        // 约束水平居中
        /// @param iconView                  要约束的控件
        /// @param NSLayoutAttribute.CenterX 约束哪一个属性
        /// @param NSLayoutRelation.Equal    相等，大于等于，小于等于
        /// @param self                      相对于的控件
        /// @param NSLayoutAttribute.CenterX 约束相对于的控件哪一个属性
        /// @param multiplier                倍数
        /// @param constant                  偏移量
        ///
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        // 2.> 圆圈
        circleIcon.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: houseImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleIcon, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: houseImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 3.> message的label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: circleIcon, attribute: .Bottom, multiplier: 1, constant: 16))
        // 约束宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        
        
        // 4.> 注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Leading, relatedBy: .Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 5.> 登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Trailing, relatedBy: .Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: registerBtn, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        // 5.> 阴影
        // VFL: 可视化格式语言
        // | : 边界
        // H : 约束方向
        // metrics : 用于设置格式字符串里面的 `()` 里面的数值
        // views: 用于设置格式字符串里面的 `[]` 里面的View
        maskImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: [], metrics: nil, views: ["view" : maskImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-(offsetY)-[registerButton]", options: [], metrics: ["offsetY": -35], views: ["view": maskImage, "registerButton": registerBtn]))
        
    }
}
